/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "keyboard.h"
#include "syslog.h"

static void keyboard_suspend(Device_t* dev)
{
	DeviceKeyboard* key = (DeviceKeyboard*)dev;
	key->Protected.ops->disable(key);
}

static void keyboard_resume(Device_t* dev)
{
	DeviceKeyboard* key = (DeviceKeyboard*)dev;
	key->Protected.ops->enable(key);
}

static DeviceKeyboard _keyboard =
{
	.super = {
		.resume = keyboard_resume,
		.suspend = keyboard_suspend
	}
};

static Key_t* keyhead = NULL;                              /*按键链表头结点*/

int DeviceKeyboardKeyCreate(Key_t* key)
{
	Key_t* keytail = keyhead;
	if (key == NULL)
		return 0;

	key->next = NULL;
	if (keyhead == NULL) {
		keyhead = key;
		return 1;
	}
	while (keytail->next != NULL)                          /*转至链尾*/
		keytail = keytail->next;
	keytail->next = key;
	return 1;
}

/*
 * @brief       获取系统滴答时钟值(通常单位是1ms)
 */
static unsigned int get_tick(void)
{
    return atomTimeGet();
}

/*
 * @brief       超时判断
 * @param[in]   start   - 起始时间
 * @param[in]   timeout - 超时时间(ms)
 */
static int is_timeout(unsigned int start, unsigned int timeout)
{
    return get_tick() - start > timeout;
}

/*******************************************************************************
 * @brief       按键扫描处理
 * @return      none
 ******************************************************************************/
static void KeyScanProcess(void)
{
    Key_t* k;
    for (k = keyhead; k != NULL; k = k->next) {
        if (_keyboard.Protected.ops->readkey(&_keyboard, k)) {
            if (k->Protected.tick) {
                if (is_timeout(k->Protected.tick, LONG_PRESS_TIME))    /*长按判断 */
					_keyboard.Protected.key_event(&_keyboard, k, KEY_LONG_DOWN, get_tick() - k->Protected.tick);
            }
            else {
                k->Protected.tick = get_tick();                        /*记录首次按下时间*/
            }
        }
        else if (k->Protected.tick) {
            if (is_timeout(k->Protected.tick, LONG_PRESS_TIME)) {      /*长按释放 */
				_keyboard.Protected.key_event(&_keyboard, k, KEY_LONG_DOWN, get_tick() - k->Protected.tick);
            }

            /*短按释放操作 ---------------------------------------------------*/
            if (is_timeout(k->Protected.tick, KEY_DEBOUNCE_TIME) &&
                !is_timeout(k->Protected.tick, LONG_PRESS_TIME)) {
				_keyboard.Protected.key_event(&_keyboard, k, KEY_LONG_DOWN, get_tick() - k->Protected.tick);
            }

            k->Protected.tick = 0;
        }
    }
}

void DeviceKeyboardCreate(DeviceDesc* desc, KEYBOARD_EVENT key_event, const KeyboardOps* ops)
{
	_keyboard.Protected.ops = ops;
	_keyboard.Protected.key_event = key_event;
	DeviceInstall(&_keyboard.super, desc);
}

void DeviceKeyScanProcess()
{
	KeyScanProcess();
}
